package activity.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.hontech.pizzav2.R
import mvc.model.inventory.GoodsTypeWare


class ScanPastDueItem(view: View) : RecyclerView.ViewHolder(view) {

    private val mLayout = view.findViewById<LinearLayout>(R.id.id_scan_item_past_due_layout)
    private val mTextGoodsType = view.findViewById<TextView>(R.id.id_scan_item_text_goods_type)
    private val mTextName = view.findViewById<TextView>(R.id.id_scan_item_text_name)
    private val mTextPrice = view.findViewById<TextView>(R.id.id_scan_item_text_price)

    fun setUp(model: GoodsTypeWare) {

        /**
         * 过期商品
         */
        if (model.pastDueWare != null) {
            mTextGoodsType.text = model.pastDueWare!!.goodsType
            mTextName.text = model.pastDueWare!!.barCode
            mTextPrice.text = model.pastDueWare!!.message
            mLayout.setBackgroundColor(Color.RED)
            return
        }

        mTextGoodsType.text = model.cargo.name
        mTextName.text = model.name
        mTextPrice.text = model.price
        if (!model.cargo.isNotOverdue()) {
            mLayout.setBackgroundColor(Color.RED)
        } else {
            mLayout.setBackgroundColor(Color.BLUE)
        }
    }
}

class ScanPastDueAdapter(val models: ArrayList<GoodsTypeWare>) :
    RecyclerView.Adapter<ScanPastDueItem>() {

    override fun onBindViewHolder(item: ScanPastDueItem, position: Int) {
        item.setUp(models[position])
    }

    override fun getItemCount(): Int {
        return models.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, position: Int): ScanPastDueItem {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_scan_past_due, parent, false)
        return ScanPastDueItem(view)
    }
}

